package com.example.demo.hcontroller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.pojo.Tongzhi;
import com.example.demo.service.ITongzhiService;

@Controller
@RequestMapping(value = "/hTongzhi")
public class HTongzhiController {

    @Autowired
    private ITongzhiService TongzhiService;

    @RequestMapping("/toTongzhi")
    private String toTongzhi(Model model, Tongzhi Tongzhi){
        List<Tongzhi> list=TongzhiService.selectTongzhi();
        model.addAttribute("tongzhilist", list);
        return "tongzhi";
    }

    @RequestMapping("/toaddTongzhi")
    private String toaddTongzhi(Model model, Tongzhi Tongzhi){
        List<Tongzhi> list=TongzhiService.selectTongzhi();
        model.addAttribute("tongzhilist", list);
        return "addtongzhi";
    }
    
    @RequestMapping("/addTongzhi")
    private String addTongzhi(@RequestParam(value="file") MultipartFile file,Model model, Tongzhi Tongzhi,String title,String content,String community) throws IllegalStateException, IOException{
        String path1 = System.getProperty("user.dir");//项目根目录
        String filePath = path1 + "\\src\\main\\resources\\static\\images";
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename:" + originalFilename);
        String newFileName = UUID.randomUUID() + originalFilename;
        File targetFile = new File(filePath, newFileName);
        file.transferTo(targetFile);
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(df.format(new Date()));
    	Tongzhi.setContent(content);
    	Tongzhi.setTitle(title); 
    	Tongzhi.setTime(df.format(new Date()));
    	Tongzhi.setImg("http://localhost:8080/images/"+newFileName);
    	TongzhiService.insertSelective(Tongzhi);
        List<Tongzhi> list=TongzhiService.selectTongzhi();
        model.addAttribute("tongzhilist", list);
        return "redirect:toTongzhi";
    }
    
    @RequestMapping("/toupdateTongzhi")
    private String toupdateTongzhi(Model model, Tongzhi Tongzhi,Integer id){
    	Tongzhi=TongzhiService.selectByPrimaryKey(id);
        model.addAttribute("tongzhi", Tongzhi);
        return "updatetongzhi";
    }
    
    @RequestMapping("/updateTongzhi")
    private String updateTongzhi(Model model, Tongzhi Tongzhi,Integer id,String title,String content,String price){
        System.out.println("id:"+id+" title:"+title+" content:"+content);
    	Tongzhi.setContent(content);
    	Tongzhi.setTitle(title);
    	Tongzhi.setId(id);
    	TongzhiService.updateByPrimaryKeySelective(Tongzhi);
        return "redirect:toTongzhi";
    }
    
    @RequestMapping("/deleteTongzhi")
    private String deleteTongzhi(Model model, Tongzhi Tongzhi,Integer id){
    	TongzhiService.deleteByPrimaryKey(id);
        return "redirect:toTongzhi";
    }

}
